package com.zfsoft.zfoa.onduty.wage.dao;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.zfsoft.zfoa.dao.BasicDAO;
import com.zfsoft.zfoa.onduty.wage.entitys.Wagechild;
public class WagechildDAO extends BasicDAO {
	public Wagechild getInstance(String bh) {
		Wagechild wagechild = new Wagechild();
		try {
			String sql = "select * from wagechild where id=?";
			conn = this.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, bh);
			rs = ps.executeQuery();
			if (rs.next()) {
				wagechild.setId(rs.getString("id"));
				wagechild.setUserid(rs.getString("userid"));
				wagechild.setUsername(rs.getString("username"));
				wagechild.setAllday(rs.getString("allday"));
				wagechild.setAllmoney(rs.getString("allmoney"));
				wagechild.setDaytime(rs.getString("daytime"));
				wagechild.setDaymoney(rs.getString("daymoney"));
				wagechild.setNighttime(rs.getString("nighttime"));
				wagechild.setNightmoney(rs.getString("nightmoney"));
				wagechild.setSum(rs.getString("sum"));
				wagechild.setFid(rs.getString("fid"));
			}
			closeStmt();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConn();
		}
		return wagechild;
	}
	public Boolean update(String bh, Wagechild wagechild) {
		Boolean res = true;
		String sql = "";
		try {
			sql = "UPDATE wagechild SET id=?,userid=?,username=?,allday=?,allmoney=?,daytime=?,daymoney=?,nighttime=?,nightmoney=?,sum=?,fid=? WHERE id=?";
			conn = this.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, wagechild.getId());
			ps.setString(2, wagechild.getUserid());
			ps.setString(3, wagechild.getUsername());
			ps.setString(4, wagechild.getAllday());
			ps.setString(5, wagechild.getAllmoney());
			ps.setString(6, wagechild.getDaytime());
			ps.setString(7, wagechild.getDaymoney());
			ps.setString(8, wagechild.getNighttime());
			ps.setString(9, wagechild.getNightmoney());
			ps.setString(10, wagechild.getSum());
			ps.setString(11, wagechild.getFid());
			ps.setString(12, bh);
			ps.executeUpdate();
			closeStmt();
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println(sql);
				e1.printStackTrace();
			}
			res = false;
		} finally {
			try {
				conn.commit();
			} catch (SQLException e) {
				System.out.println(sql);
				e.printStackTrace();
			}
			closeConn();
		}
		return res;
	}
	public List<Wagechild> getListByFid(String id){
		List<Wagechild> list =new ArrayList<Wagechild>();
		
		try {
			String sql = "select * from wagechild where fid=?";
			conn = this.getConn();
			ps = conn.prepareStatement(sql);
			ps.setString(1, id);
			rs = ps.executeQuery();
			while (rs.next()) {
				Wagechild wagechild = new Wagechild();
				wagechild.setId(rs.getString("id"));
				wagechild.setUserid(rs.getString("userid"));
				wagechild.setUsername(rs.getString("username"));
				wagechild.setAllday(rs.getString("allday"));
				wagechild.setAllmoney(rs.getString("allmoney"));
				wagechild.setDaytime(rs.getString("daytime"));
				wagechild.setDaymoney(rs.getString("daymoney"));
				wagechild.setNighttime(rs.getString("nighttime"));
				wagechild.setNightmoney(rs.getString("nightmoney"));
				wagechild.setSum(rs.getString("sum"));
				wagechild.setFid(rs.getString("fid"));
				list.add(wagechild);
			}
			closeStmt();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConn();
		}
		return list;
	
	}
	public Boolean insert(Wagechild wagechild) {
		Boolean res = true;
		String sql = "";
		try {
			sql = "insert into wagechild(id,userid,username,allday,allmoney,daytime,daymoney,nighttime,nightmoney,sum,fid) values(?,?,?,?,?,?,?,?,?,?,?)";
			conn = this.getConn();
			conn.setAutoCommit(false);
			ps = conn.prepareStatement(sql);
			ps.setString(1, wagechild.getId());
			ps.setString(2, wagechild.getUserid());
			ps.setString(3, wagechild.getUsername());
			ps.setString(4, wagechild.getAllday());
			ps.setString(5, wagechild.getAllmoney());
			ps.setString(6, wagechild.getDaytime());
			ps.setString(7, wagechild.getDaymoney());
			ps.setString(8, wagechild.getNighttime());
			ps.setString(9, wagechild.getNightmoney());
			ps.setString(10, wagechild.getSum());
			ps.setString(11, wagechild.getFid());
			ps.executeUpdate();
			closeStmt();
		} catch (SQLException e) {
			System.out.println(sql);
			e.printStackTrace();
			try {
				conn.rollback();
			} catch (SQLException e1) {
				System.out.println(sql);
				e1.printStackTrace();
			}
			res = false;
		} finally {
			try {
				conn.commit();
			} catch (SQLException e) {
				System.out.println(sql);
				e.printStackTrace();
			}
			closeConn();
		}
		return res;
	}
}